package com.jule.teenpatti.room.service;

import JoloProtobuf.RoomSvr.JoloRoom;
import com.jule.teenpatti.room.dao.DBUtil;
import com.jule.teenpatti.room.dao.bean.DeskConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @author xujian
 */
public class DeskConfigHolder {
    private final static Logger logger = LoggerFactory.getLogger(DeskConfigHolder.class);
    private static final ConcurrentSkipListMap<String, JoloRoom.JoloRoom_RoomInfo> ROOM_INFO_MAP = new ConcurrentSkipListMap<>();//缓存的房间列表

    private static Map<String, DeskConfigUnit> TABLE_MAP = new ConcurrentHashMap<>();//房间Id，桌子列表
    //private static Map<String, JoloRoom.JoloRoom_TableInfo> TABLE_MAP2 = new ConcurrentHashMap<>();//桌子号，桌子静态配置

    private static class SingletonHolder {
        protected static final DeskConfigHolder instance = new DeskConfigHolder();
    }

    public static final DeskConfigHolder getInstance() {
        return SingletonHolder.instance;
    }

    private DeskConfigHolder() {
        List<DeskConfig> configs = DBUtil.getDeskConfigFromDb();
        logger.info("load " + configs.size() + " room config");
        configs.forEach(deskConfig -> {
            logger.info(deskConfig.toString());
            //生成房间缓存
            if (!ROOM_INFO_MAP.containsKey(deskConfig.getId())) {
                ROOM_INFO_MAP.put(deskConfig.getId(), JoloRoom.JoloRoom_RoomInfo.newBuilder().setRoomId(deskConfig.getId()).setRoomName(deskConfig.getRoom_desc()).build());
            }

            List<JoloRoom.JoloRoom_TableInfo> tableList = new ArrayList<>();
            List<String> tableIds = new ArrayList<>();
            for (int i = deskConfig.getDesk_begin(); i <= deskConfig.getDesk_end(); i++) {
                String tableId = i + "";
                tableIds.add(tableId);
                tableList.add(JoloRoom.JoloRoom_TableInfo.newBuilder().setTableId(tableId)
                        .setTableName(tableId)
                        .setRoomId(deskConfig.getId())
                        .setStakes(deskConfig.getLimit_bet())
                        .setMaxBet(deskConfig.getMax_bet())
                        .setPlayerCnt(0)
                        .setWaitingCnt(0)
                        .setBandar(deskConfig.getZhuang_limit()).build());
            }

            if (!TABLE_MAP.containsKey(deskConfig.getId())) {
                TABLE_MAP.put(deskConfig.getId(), new DeskConfigUnit(tableIds, tableList));
            } else {
                TABLE_MAP.get(deskConfig.getId()).addDeskList(tableList, tableIds);
            }
        });
    }

    /**
     * @param roomId
     * @return
     */
    public DeskConfigUnit getDeskConfigUnit(String roomId) {
        if (!TABLE_MAP.containsKey(roomId)) {
            return null;
        }
        return TABLE_MAP.get(roomId);
    }

    /**
     * 缓存的桌子列表
     */
    public static final class DeskConfigUnit {
        private List<JoloRoom.JoloRoom_TableInfo> deskList = new ArrayList<>();
        private List<String> deskIds = new ArrayList<>();
        private String[] ids;//真正的jedis 参数缓存

        private void makeListToArray() {
            String[] _ids = new String[deskIds.size()];
            for (int i = 0; i < deskIds.size(); i++) {
                _ids[i] = deskIds.get(i);
            }
            this.ids = _ids;
        }

        public DeskConfigUnit(List<String> ids, List<JoloRoom.JoloRoom_TableInfo> arrayList) {
            this.deskIds.addAll(ids);
            this.deskList.addAll(arrayList);
            makeListToArray();
        }

        public List<JoloRoom.JoloRoom_TableInfo> getDeskList() {
            return deskList;
        }

        public void addDeskList(List<JoloRoom.JoloRoom_TableInfo> arrayList, List<String> ids) {
            this.deskList.addAll(arrayList);
            this.deskIds.addAll(ids);
            makeListToArray();
        }

        /**
         * @return
         */
        public String[] getDeskIds() {
            return this.ids;
        }
    }

    /**
     * 获取房间列表
     *
     * @return
     */
    public Collection<JoloRoom.JoloRoom_RoomInfo> getRoomInfoList() {
        return ROOM_INFO_MAP.values();
    }
}
